
************************************
*** TABLE 1: Effect of EMR Adoption on Clinic-Level Patient Outcomes
*** TABLE 3: EMR Heterogeneous Treatment Effect by Clinic Type
************************************

* data and cd
clear all
set more off
set matsize 11000
set cformat %5.3f
local date = 20240909

cap cd "C:\Users\laura\Dropbox (Frischsenteret)\Research Projects\CGC 2017\EMRI\REPLICATION"
cap cd "C:\Users\laurader\Dropbox (Frischsenteret)\Research Projects\CGC 2017\EMRI\REPLICATION"
cap cd "C:\Users\pongelup\Dropbox\Assistant Professor\Research\Research\R - 2017_12_01_EMR Malawi\CGC 2017\EMRI\REPLICATION"


use Data/Analysis/Data_ClinicYears, clear


keep YearsPostEMR EMRyear Datey VisitSite logInCare* logDeath* logNew* logReturn* Post hospital urban large

gen Ei=EMRyear
gen t=Datey
gen K = t-Ei 								
gen D = K>=0 & Ei!=. 

gen i=VisitSite
tsset i t


cap erase Output/Tables/Table01.xls
cap erase Output/Tables/Table01.txt

foreach x of varlist logDeath logDeathM logDeathF logDeathAge_* logInCare logInCareM logInCareF logInCareAge_* logNew logNewM logNewF logNewAge_* logReturn logReturnM logReturnF logReturnAge_* {
	reghdfe `x' Post, a(i t) cluster(i)
		estimates store e_`x'_post
		outreg2 using Output/Tables/Table01.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) keep(Post) addtext(Year FE, Y, Clinic FE, Y, TWFE, Y, Borusyak et al, N) 	
}


foreach x of varlist logDeath logDeathM logDeathF logDeathAge_* logNew logNewM logNewF logNewAge_*  {
	did_imputation `x' i t Ei, pretrend(0) 
		outreg2 using Output/Tables/Table01.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) addtext(Year FE, Y, Clinic FE, Y, TWFE, N, Borusyak et al, Y)
		
}

gen Post_large=Post*large
gen Post_nlarge=Post*(1-large)
gen Post_urban=Post*urban
gen Post_nurban=Post*(1-urban)
gen Post_hospital=Post*hospital
gen Post_nhospital=Post*(1-hospital)

cap erase Output/Tables/Table03.xls
cap erase Output/Tables/Table03.txt

local date = 20240801
foreach x of varlist logDeath logInCare logNew logReturn {
	reghdfe `x' Post_hospital Post_nhospital, a(i t) cluster(i)
	estimates store e_`x'_posthospital
			outreg2 using Output/Tables/Table03.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) addtext(Year FE, Y, Clinic FE, Y, TWFE, Y, Borusyak et al, N) 	

	reghdfe `x' Post_large Post_nlarge, a(i t) cluster(i)
	estimates store e_`x'_postlarge
			outreg2 using Output/Tables/Table03.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) addtext(Year FE, Y, Clinic FE, Y, TWFE, Y, Borusyak et al, N) 	
			
	reghdfe `x' Post_urban Post_nurban, a(i t) cluster(i)
	estimates store e_`x'_posturban
			outreg2 using Output/Tables/Table03.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) addtext(Year FE, Y, Clinic FE, Y, TWFE, Y, Borusyak et al, N) 	
}

foreach x of varlist logDeath logNew  {
	did_imputation `x' i t Ei if hospital==1, pretrend(0) 
			outreg2 using Output/Tables/Table03.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) addtext(Year FE, Y, Clinic FE, Y, TWFE, N, Borusyak et al, Y)

	did_imputation `x' i t Ei if hospital==0, pretrend(0) 
			outreg2 using Output/Tables/Table03.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) addtext(Year FE, Y, Clinic FE, Y, TWFE, N, Borusyak et al, Y)

	did_imputation `x' i t Ei if large==1, pretrend(0) 
			outreg2 using Output/Tables/Table03.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) addtext(Year FE, Y, Clinic FE, Y, TWFE, N, Borusyak et al, Y)

	did_imputation `x' i t Ei if large==0, pretrend(0) 
			outreg2 using Output/Tables/Table03.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) addtext(Year FE, Y, Clinic FE, Y, TWFE, N, Borusyak et al, Y)

	did_imputation `x' i t Ei if urban==1, pretrend(0) 
			outreg2 using Output/Tables/Table03.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) addtext(Year FE, Y, Clinic FE, Y, TWFE, N, Borusyak et al, Y)

	did_imputation `x' i t Ei if urban==0, pretrend(0) 
			outreg2 using Output/Tables/Table03.xls, alpha(0.01, 0.05, 0.1) symbol(***, **, *) addtext(Year FE, Y, Clinic FE, Y, TWFE, N, Borusyak et al, Y)

}
